package br.com.forca.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IOUtils {
	
	private WordUtils wu;
	
	public IOUtils(){
		wu = new WordUtils();
	}

	public String getUserInputString(String prompt) {

		String inputLine = null;
		System.out.println(prompt + " ");

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			inputLine = br.readLine().replaceAll("[^a-zA-Z]", "*");

			if (inputLine.isEmpty() || inputLine.contains("*")) {
				System.out.println("Entrada Inválida, não utilize números,caracteres especiais ou cadeias vazias!!!");
				inputLine = getUserInputString(prompt);
			}

		} catch (IOException ex) {
			System.out.println("IOException: " + ex);
		}

		return inputLine.toUpperCase();
	}

	
	
	public char getUserInputChar(String prompt) {
		String inputline = null;
		char c = ' ';
		System.out.println(prompt + " ");

		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));
			inputline = in.readLine().toUpperCase().replaceAll("[^a-zA-Z]", "*");

			if (inputline.isEmpty() || inputline.length()>1 || inputline.contains("*")) {
				System.out.println("Erro na entrada, Digite apenas 1(uma)letra!!!");
				c = getUserInputChar(prompt);
				c = wu.trataLetras(c + "");
			} else {
				c = wu.trataLetras(inputline);
				c = inputline.charAt(0);
			}

		} catch (IOException ex) {
			System.out.println("IOException: " + ex);
		}
		return c;
	}

	
	
	public int getUserInputNumber(String prompt) {
		String inputline = null;
		int n1 = 0;
		System.out.println(prompt + " ");

		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			inputline = in.readLine().toUpperCase().replaceAll("[^0-9]", "*");

			if (inputline.isEmpty() || inputline.length() > 1 || inputline.contains("*")) {
				System.out.println("Erro na entrada, Digite apenas 1(um) digito numérico!!!");
				n1 = getUserInputNumber(prompt);
			} else {
				n1 = Integer.parseInt(inputline);
			}

		} catch (IOException ex) {
			System.out.println("IOException: " + ex);
		}
		return n1;
	}

	
	

}
